//
//  DHMyCouponsController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/10.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHMyCouponsController.h"
#import "DHS2_CouponsCell.h"
#import "DHCouponMJRefreshAutoNormalFooter.h"
#import "DHMembCheckController.h"
#import "DHS2_BuyMemberController.h"

@interface DHMyCouponsController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *couponsArray;
@property (assign, nonatomic) NSInteger pageIndex;

@end

@implementation DHMyCouponsController

- (NSMutableArray *)couponsArray{
    if (!_couponsArray) {
        _couponsArray = [NSMutableArray array];
    }
    return _couponsArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"惠享Dresshere";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_CouponsCell" bundle:nil] forCellReuseIdentifier:kDHS2_CouponsCell];
    self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadCoupons)];
    self.tableView.mj_footer = [DHCouponMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreCoupons)];
    
    [self loadCoupons];
}

- (void)loadCoupons{
    self.pageIndex = 0;
    [self.tableView.mj_footer resetNoMoreData];
    [[DHNetAPIClient shareDHNetAPIClient] request_couponsWithParams:@{@"pageIndex":@(self.pageIndex),@"pageSize":@(20),@"used":@"false"} andBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.couponsArray removeAllObjects];
        
        NSArray *tempArr = [data objectForKey:@"coupons"];
        if (self.isOrder) {
            [tempArr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                CGFloat condition = [[obj objectForKey:@"condition"] floatValue];
                if ([[obj objectForKey:@"type"] isEqualToString:@"现金券"] && condition < self.orderPrice) {
                    [self.couponsArray addObject:obj];
                }
            }];
        }else{
            [self.couponsArray addObjectsFromArray:tempArr];
        }
        if (self.couponsArray.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            self.pageIndex += 1;
        }
        
        [self.tableView reloadData];
    }];
}

- (void)loadMoreCoupons{
    [[DHNetAPIClient shareDHNetAPIClient] request_couponsWithParams:@{@"pageIndex":@(self.pageIndex),@"pageSize":@(20),@"used":@"false"} andBlock:^(id data, NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [self.couponsArray addObjectsFromArray:[data objectForKey:@"coupons"]];
        if (self.couponsArray.count < 20 * (self.pageIndex + 1)) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            self.pageIndex += 1;
        }
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate and UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.couponsArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHS2_CouponsCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_CouponsCell forIndexPath:indexPath];
    [cell updateCell:self.couponsArray[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isOrder) {
        NSDictionary *dic = self.couponsArray[indexPath.row];
        [[DHNetAPIClient shareDHNetAPIClient] request_order_couponWithParams:@{@"order_id":self.order_id,@"coupon_id":[dic objectForKey:@"_id"]} andBlock:^(id data, NSError *error) {
            postN(@"use_coupon");
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }else{
        NSDictionary *dic = self.couponsArray[indexPath.row];
        if ([[dic objectForKey:@"type"] isEqualToString:@"现金券"]) {
            [self.tabBarController setSelectedIndex:2];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else{
            if (![GlobalManager shareGlobalManager].vip.vip) {
                DHS2_BuyMemberController *buyController = [[DHS2_BuyMemberController alloc] init];
                [self.navigationController pushViewController:buyController animated:YES];
            }else{
                DHMembCheckController *vc = [[DHMembCheckController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
    }
}

@end
